import 'package:get/get.dart';
import 'package:xinxuan/http/http_util_service.dart';
import 'package:xinxuan/vo/common_vo/common_pagination_vo.dart';
import 'package:xinxuan/vo/seckill_order_list_vo.dart';

class SeckillOrderPageController extends GetxController {
  
  /// tab 下标
  var tabIndex = 0.obs;
  List<int> statusArr = [-1, 1, 2, 3, 4];
  /// 订单数据
  var dataArr = <SeckillOrderListVo>[
    SeckillOrderListVo(data: [], meta: SeckillOrderListMeta(teams: [], pagination: CommonPagination(total: 0, currentPage: 1))),
    SeckillOrderListVo(data: [], meta: SeckillOrderListMeta(teams: [], pagination: CommonPagination(total: 0, currentPage: 1))),
    SeckillOrderListVo(data: [], meta: SeckillOrderListMeta(teams: [], pagination: CommonPagination(total: 0, currentPage: 1))),
    SeckillOrderListVo(data: [], meta: SeckillOrderListMeta(teams: [], pagination: CommonPagination(total: 0, currentPage: 1))),
    SeckillOrderListVo(data: [], meta: SeckillOrderListMeta(teams: [], pagination: CommonPagination(total: 0, currentPage: 1))),
  ].obs;

  /// 展示筛选
  var showScreenBody = false.obs;
  /// 已选中空间
  var screenArr = [].obs;

  @override
  void onReady() {
    Map<String, dynamic> argumentsMap = Get.arguments;
    tabIndex.value = argumentsMap['tabIndex'];
    update();
    getSeckillOrderList();
    super.onReady();
  }

  /// 获取订单列表
  getSeckillOrderList() async {
    // EasyLoading.show();
    // for (int i = 0; i < statusArr.length; i++) {
    //   SeckillOrderListVo tempVo = await HttpUtilService.getSeckillOrderList(status: statusArr[i], page: dataArr[i].meta!.pagination!.currentPage!, loading: false);
    //   dataArr[i] = tempVo;
    //   update();
    //   EasyLoading.dismiss();
    // }

    // dataArr[tabIndex.value].meta!.pagination!.currentPage = 1;
    // SeckillOrderListVo tempVo = await HttpUtilService.getSeckillOrderList(status: statusArr[tabIndex.value], page: dataArr[tabIndex.value].meta!.pagination!.currentPage!);
    // dataArr[tabIndex.value] = tempVo;
    // update();
    String teamId = _getTeamIds();
    dataArr[tabIndex.value].meta!.pagination!.currentPage = 1;
    SeckillOrderListVo tempVo = await HttpUtilService.getSeckillOrderList(status: statusArr[tabIndex.value], teamId: teamId, page: dataArr[tabIndex.value].meta!.pagination!.currentPage!, loading: true);
    dataArr[tabIndex.value] = tempVo;
    update();

    // for (int i = 0; i < statusArr.length; i++) {
    //   SeckillOrderListVo tempVo = await HttpUtilService.getSeckillOrderList(status: statusArr[i], page: dataArr[i].meta!.pagination!.currentPage!, loading: false);
    //   dataArr[i] = tempVo;
    // }
    // update();

  }

  /// 加载更多订单列表
  onloadSeckillOrderList() async {    
    if (dataArr[tabIndex.value].meta!.pagination!.total! <= dataArr[tabIndex.value].data!.length) {
      return;
    }
    String teamId = _getTeamIds();
    dataArr[tabIndex.value].meta!.pagination!.currentPage = dataArr[tabIndex.value].meta!.pagination!.currentPage! + 1;
    SeckillOrderListVo tempVo = await HttpUtilService.getSeckillOrderList(status: statusArr[tabIndex.value], teamId: teamId, page: dataArr[tabIndex.value].meta!.pagination!.currentPage!, loading: false);
    dataArr[tabIndex.value].data!.addAll(tempVo.data!);
    update();
  }

  /// 获取Tab某项订单列表
  getSeckillTabItemOrderList(int index) async {
    String teamId = _getTeamIds();
    dataArr[index].meta!.pagination!.currentPage = 1;
    SeckillOrderListVo tempVo = await HttpUtilService.getSeckillOrderList(status: statusArr[index], teamId: teamId, page: dataArr[index].meta!.pagination!.currentPage!, loading: false);
    dataArr[index] = tempVo;
    update();
  }

  /// 修改已选中的项
  changeScreenArr(int id) {
    if (screenArr.contains(id)) {
      screenArr.remove(id);
    } else {
      screenArr.add(id);
    }
  }

  String _getTeamIds() {
    String tempId = '';
    screenArr.forEach((item) {
      tempId += '$item,';
    });
    if (screenArr.length > 0) {
      tempId = tempId.substring(0, tempId.length - 1);
    }
    return tempId;
  }

}